﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using MovieRental_Presentation.Helpers;
using MovieRental.EnhancedEntities;
using System.Windows.Controls;
using System.Windows.Navigation;
using MovieRental.Entities;
using System.Collections;
using MovieRental.BuisnessComponents.Exceptions;

namespace MovieRental_Presentation.Templates
{
    partial class TemplatesResourceDictionary : ResourceDictionary
    {
        public TemplatesResourceDictionary()
        {
            InitializeComponent();
        }
		
		private void addToCartButton_Click(object sender, RoutedEventArgs args)
		{
			Edition edition = (sender as Button).DataContext as Edition;
			var cart = (App.Current as App).CartDataView.CurrentCart.Entity;
            var globalResources = (Application.Current as App).Resources;
            try
            {
                ComponentManager.Instance.LoanCartComponent.AddEditionToCart(cart, edition.Entity);
            }
            catch (ItemDuplicateException<Guid>)
            {
                string message = string.Format((string)globalResources["addToCartFailedMessage"], edition.MovieTitle);
                MessageBox.Show(message, "The edition is already in the cart.", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (NotEnoughException)
            {
                string message = string.Format((string)globalResources["zeroInStockMessage"], edition.MovieTitle);
                MessageBox.Show(message, "Nothing in stock.", MessageBoxButton.OK, MessageBoxImage.Information);
            }
		}
		
		private void returnButton_Click(object sender, RoutedEventArgs args)
		{
            var loans = (sender as Button).DataContext as IList;
            var page = new ReturnDialog(new List<Loan>(loans.OfType<Loan>()));
            var navigator = NavigationService.GetNavigationService(sender as DependencyObject);
            navigator.Navigate(page);
		}

        private void returnAllButton_Click(object sender, RoutedEventArgs args)
        {
            var loans = (sender as Button).DataContext as IList;
            var page = new ReturnDialog(new List<Loan>(loans.OfType<Loan>()));
            var navigator = NavigationService.GetNavigationService(sender as DependencyObject);
            navigator.Navigate(page);
        }
    }
}
